﻿using Minotaur.App.Core.Redis;
using Minotaur.App.Domains.Dto;
using Minotaur.App.Domains.Model.Personal;
using Prism.Regions;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace Minotaur.App.Core.Mvvm
{
    public class RegionViewModelBase : ViewModelBase, INavigationAware, IConfirmNavigationRequest
    {
        public RegionViewModelBase(IRegionManager regionManager, IDbContext dbContext, IRedisClient _redisClient)
        {
            RegionManager = regionManager;
            this.dbContext = dbContext;
            this.redisClient = _redisClient; 
        }

        /// <summary>
        /// 初始化员工缓存
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void InitEmpCache()
        {
            var lan = AppDataContext.GlobalUserModel.Language;
            var hashKey = $"{AppDataContext.GlobalUserModel.TenementCode}$EmpCache";
            List<EmployeeInfoCacheDto> cacheDtos = new List<EmployeeInfoCacheDto>();
         
            if (redisClient.Exists(hashKey))
            {
                cacheDtos = redisClient.Get<List<EmployeeInfoCacheDto>>(hashKey);
            }
            else
            {

                var res = this.dbContext.BusinessDb.Queryable<Employee>().Includes<EmployeeRes>(x => x.Res).ToList();
                foreach (var item in res)
                {
                    EmployeeInfoCacheDto employeeInfoCacheDto = new EmployeeInfoCacheDto()
                    {
                        EmployeeId = item.AutoId,
                        EmployeeCode = item.EmpCode,
                    };
                    foreach (var re in item.Res)
                    {
                        employeeInfoCacheDto.Chinese = item.Res.FirstOrDefault(x => x.Lan == 0)?.Value;
                        employeeInfoCacheDto.English = item.Res.FirstOrDefault(x => x.Lan == 1)?.Value;
                    }
                    cacheDtos.Add(employeeInfoCacheDto);
                }
                if (cacheDtos.Any())
                {
                    redisClient.Add<List<EmployeeInfoCacheDto>>(hashKey, cacheDtos, DateTime.Now.AddDays(1));
                }
            }
            if (cacheDtos != null && cacheDtos.Any())
            {
                foreach (var item in cacheDtos)
                {
                    this.EmpCache[item.EmployeeId] = lan == "zh-cn" ? item.Chinese : item.English;
                }
            }
        }

        protected IRegionManager RegionManager { get; private set; }
        protected IDbContext dbContext { get; private set; }
        public Dispatcher Dispatcher { get; set; }

        private readonly IRedisClient redisClient;
        public Dictionary<long, string> EmpCache { get; private set; }=new Dictionary<long, string>();


        public virtual void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
            continuationCallback(true);
        }

        public virtual bool IsNavigationTarget(NavigationContext navigationContext) => true;

        public virtual void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        public virtual void OnNavigatedTo(NavigationContext navigationContext)
        {

        }
        /// <summary>
        /// 获取缓存信息
        /// </summary>
        /// <param name="EmpId"></param>
        /// <returns></returns>
        public string GetEmpCache(int EmpId)
        {
            this.InitEmpCache();
            if (this.EmpCache.ContainsKey(EmpId))
            {
                return this.EmpCache[EmpId];
            }
            else
            {
                this.redisClient.Remove($"{AppDataContext.GlobalUserModel.TenementCode}$EmpCache");
                this.InitEmpCache();
                return this.EmpCache[EmpId];
            }
        }
    }
}
